// 导入用户集合
const {
  News
} = require('../../../model/news');
const {
  User
} = require('../../../model/user');
const moment = require('moment')
const {
  decodeToken
} = require('../../../middleGuard/jwt.js')

module.exports = async (req, res) => {
  const userId = decodeToken(req.headers.authorization).userId
  News.create({
    ...req.body,
    user: userId,
    createTime: moment().format('YYYY-MM-DD HH:mm:ss')
  }).then(async (item) => {
    const user = await User.findOne({_id: item.user})
    item.user = user
    res.json({
      data: item,
      message: '添加成功！'
    })
  }).catch((err) => {
    res.status(403)
    res.json({
      log: err,
      message: '添加失败！'
    })
  })
}
